//
//  ShopsViewController.m
//  何五路
//
//  Created by _ADY on 15/7/8.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "ShopsViewController.h"

@interface ShopsViewController ()

@end

static LoadingView *loading = nil;

#define aHight  50
@implementation ShopsViewController
@synthesize shopsId,allArray;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;
    
    UISearchBar *m_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(100, 100, 200, 41)];
    m_searchBar.delegate = self;
    m_searchBar.backgroundColor = [UIColor clearColor];
    m_searchBar.placeholder = (@"搜索商铺内商品");
    m_searchBar.keyboardType =  UIKeyboardTypeDefault;
    [self.navigationItem setTitleView:m_searchBar];
    for (UIView *view in m_searchBar.subviews) {//去除背景色
        
        if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0) {
            [[view.subviews objectAtIndex:0] removeFromSuperview];
            break;
        }
    }
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ShopsFL"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    
    mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenMySize.size.width, screenMySize.size.height-aHight) style:UITableViewStyleGrouped];
    mTableView.delegate = self;
    mTableView.dataSource = self;
    mTableView.sectionHeaderHeight = 0.0;
    mTableView.sectionFooterHeight = 0.0;
    mTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:mTableView];
    
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];//清除多余分割线
    [mTableView setTableFooterView:v];
    
    [self barView];

}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    [self gotoShopsSearch];
    searchBar.showsCancelButton = NO;
    return YES;
}

-(void)barView
{
    UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, screenMySize.size.height-aHight, screenMySize.size.width, aHight)];
    bView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bView];
    
    UIView *b1View = [[UIView alloc] initWithFrame:CGRectMake(screenMySize.size.width/3, screenMySize.size.height-aHight+10, .5, aHight)];
    b1View.backgroundColor = [UIColor colorWithWhite:.8 alpha:1];
    [self.view addSubview:b1View];
    
    UIView *b2View = [[UIView alloc] initWithFrame:CGRectMake(screenMySize.size.width*2/3, screenMySize.size.height-aHight+10, .5, aHight-20)];
    b2View.backgroundColor = [UIColor colorWithWhite:.8 alpha:1];
    [self.view addSubview:b2View];
    
    NSArray *tArray = [NSArray arrayWithObjects:@"店铺详情",@"热门分类",@"联系客服", nil];
    for (int i = 0; i < 3; i ++)
    {
        NSString *title = [tArray objectAtIndex:i];
        UIImage *imageD = nil;
        if (i == 1)
            imageD = [UIImage imageNamed:@"Shopsbar2"];
        
        UIButton *inButton = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect frame = CGRectMake(0,0, screenMySize.size.width/3, aHight);
        [inButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        inButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+1];
        [inButton setTitle:title forState:UIControlStateNormal];
        
        [inButton setFrame:frame];
        [inButton setTitleEdgeInsets:UIEdgeInsetsMake(0, imageD.size.width, 0, 0)];
        [inButton setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        
        [inButton setImage:imageD forState:UIControlStateNormal];
        
        inButton.center = CGPointMake(screenMySize.size.width*i/3+screenMySize.size.width/6, aHight/2);
        inButton.tag = 10+i;
        [inButton addTarget:self action:@selector(aInBction:) forControlEvents:UIControlEventTouchUpInside];
        [bView addSubview:inButton];
    }
}

//@"店铺详情",@"热门分类",@"联系客服"
-(void)aInBction:(id)sender
{
    int topType = ((UIButton*)sender).tag - 10;
    if (topType == 0) {
        
        ShopsDetailsViewController *details = [[ShopsDetailsViewController alloc] init];
        details.shopsId = shopsId;
        details.allArray = allArray;
        details.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:details animated:YES];
    }
    else if (topType == 1) {

        if (shopsView == nil) {
            shopsView = [[UIView alloc] initWithFrame:CGRectMake(70, screenMySize.size.height-260, screenMySize.size.width-140, 200)];
            [self.view addSubview:shopsView];
            
            UIImage *image = [[UIImage imageNamed:@"ShopsDDK"] stretchableImageWithLeftCapWidth:80 topCapHeight:25/2];
            UIImageView *bgImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenMySize.size.width-140, 200)];
            [bgImage setImage:image];
            [shopsView addSubview:bgImage];
            
            HotListView *HotList = [[HotListView alloc] initWithFrame:CGRectMake(1, 1, screenMySize.size.width-142, 200-11)];
            shopsView.alpha = 0;
            HotList.shopsId = shopsId;
            HotList.delegate = self;
            [shopsView addSubview:HotList];
        }
        if (shopsView.alpha == 1) {
            [UIView animateWithDuration:.5 animations:^{
                shopsView.alpha = 0;
            }];
        }
        else
        {
            [UIView animateWithDuration:.5 animations:^{
                shopsView.alpha = 1;
            }];
        }

    }
    else if (topType == 2) {
        
    }
}

-(void)gotoShopsSearch
{
    ShopsSearchViewController *details = [[ShopsSearchViewController alloc] init];
    details.hidesBottomBarWhenPushed = YES;
    details.shopsId = shopsId;
    [self.navigationController pushViewController:details animated:YES];
}

//@"全部商品",@"上新",@"促销",@"店铺动态"
-(void)aInyBction:(id)sender
{
    int topType = ((UIButton*)sender).tag - 100;
    if (topType == 0) {
        [self gotoShopsSearch];
    }

    
}

//优惠券
-(void)hyjAction:(id)sender
{
    //    int topType = ((UIButton*)sender).tag - 110;
}

//广告
-(void)ADAction:(id)sender
{
    //    int topType = ((UIButton*)sender).tag - 200;
}

-(void)rightAction
{
    JKSideSlipView *oneSl = (JKSideSlipView*)[[UIApplication sharedApplication].keyWindow viewWithTag:122];
    if (oneSl != nil) {
        [oneSl removeFromSuperview];
        oneSl = nil;
    }
    ShopsClassView *screenOne = [[ShopsClassView alloc] initWithFrame:CGRectMake(0, 0, SLIP_WIDTH, screenMySize.size.height)];
    screenOne.shopsId = shopsId;
    screenOne.delegate = self;
    //侧栏
    JKSideSlipView *_sideSlipView = [[JKSideSlipView alloc]initWithSender:self];
    _sideSlipView.backgroundColor = [UIColor clearColor];
    [_sideSlipView setContentView:screenOne];
    _sideSlipView.tag = 122;
    [[UIApplication sharedApplication].keyWindow addSubview:_sideSlipView];
    [_sideSlipView hide];
}

-(void)JKSshow:(NSString*)class1Id
{
    JKSideSlipView *oneSl = (JKSideSlipView*)[[UIApplication sharedApplication].keyWindow viewWithTag:122];
    if (oneSl != nil) {
        [oneSl show];
    }
    
    [self gotoShopsSearch:class1Id];

}

-(void)HotListID:(NSString*)class1Id
{
    if (shopsView.alpha == 1) {
        [UIView animateWithDuration:.5 animations:^{
            shopsView.alpha = 0;
        }];
    }
    else
    {
        [UIView animateWithDuration:.5 animations:^{
            shopsView.alpha = 1;
        }];
    }
    [self gotoShopsSearch:class1Id];
}

-(void)gotoShopsSearch:(NSString*)class1Id
{
    ShopsSearchViewController *details = [[ShopsSearchViewController alloc] init];
    details.hidesBottomBarWhenPushed = YES;
    details.shopsId = shopsId;
    details.classId = class1Id;
    [self.navigationController pushViewController:details animated:YES];
}

-(void)gzAction
{
    NSMutableDictionary *postDic1 = [[NSMutableDictionary alloc] init];
    [postDic1 setObject:[allArray objectForKey:@"ShopId"] forKey:@"vendorId"];
    
    loading = [[LoadingView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:loading];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
         NSDictionary *ret = nil;
        if (gzBOOl) {
            ret = [SendService FavoriteShopDelete:postDic1];
        }
        else
            ret = [SendService FavoriteShopInsert:postDic1];
       
        dispatch_async(dispatch_get_main_queue(), ^{
            if (![[NSString stringWithFormat:@"%@",[ret objectForKey:@"Result"]] isEqualToString:@"error"])
            {
                if (gzBOOl) {
                    [gzButton setImage:[UIImage imageNamed:@"ShopsWGZ"] forState:UIControlStateNormal];
                }
                else
                    [gzButton setImage:[UIImage imageNamed:@"ShopsGZ"] forState:UIControlStateNormal];
                gzBOOl = !gzBOOl;
            }
            else
                [SendService alerViewMessage:[NSString stringWithFormat:@"%@",[ret objectForKey:@"Message"]]];

            [loading closeLoading];
        });
    });

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.view.backgroundColor = bgColor;

    NSMutableDictionary *postDic = [[NSMutableDictionary alloc] init];
    [postDic setObject:shopsId forKey:@"shopId"];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSDictionary *ret = [SendService GetShopInfoById:postDic];
        dispatch_async(dispatch_get_main_queue(), ^{
            allArray =  [ret objectForKey:@"Entity"];
            NSLog(@"店铺基本信息 %@",ret);
            [mTableView reloadData];

        });
    });
    
    
}

#pragma mark -
#pragma mark Table View DataSource

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    if (section == 0)
        return 0.001;
    return 10;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (allArray.count >0)
        return 3;
    return 0;
}

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
        return 100*screenMySize.size.width/320+aHight;
    else if (indexPath.section == 1)
    {
        return 70;
    }
    else if (indexPath.section == 2)
    {
        return 150;
    }
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        aTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        aTableView.showsVerticalScrollIndicator = NO;
    }
    int i = (int)[[cell.contentView subviews] count] - 1;
    for(;i >= 0 ; i--)
    {
        [[[cell.contentView subviews] objectAtIndex:i] removeFromSuperview];
    }
    cell.backgroundColor = [UIColor whiteColor];
    
    if (indexPath.section == 0)
        return [self mCell:cell aCTableView:aTableView cellForRowAtIndexPath:indexPath];
    else if (indexPath.section == 1)
        return [self mCell:cell bCTableView:aTableView cellForRowAtIndexPath:indexPath];
    else if (indexPath.section == 2)
        return [self mCell:cell cCTableView:aTableView cellForRowAtIndexPath:indexPath];
    else if (indexPath.section == 3)
        return [self mCell:cell dCTableView:aTableView cellForRowAtIndexPath:indexPath];
    else if (indexPath.section == 4)
        return [self mCell:cell eCTableView:aTableView cellForRowAtIndexPath:indexPath];
    // Configure the cell...
    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell aCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, screenMySize.size.width, 100*screenMySize.size.width/320)];
    [bgImageView setImage:[UIImage imageNamed:@"Shops_bg"]];
    [cell.contentView addSubview:bgImageView];
    
        
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(10,100*screenMySize.size.width/320-50, 100, 40)];
    [imageView1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[allArray objectForKey:@"Logo"]]]];
    [cell.contentView addSubview:imageView1];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(110,100*screenMySize.size.width/320-50, screenMySize.size.width-15-108/2-100, 30)];
    titleLabel.text = [NSString stringWithFormat:@"%@",[allArray objectForKey:@"ShopName"]];
    titleLabel.textAlignment = 0;
    titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+2];
    titleLabel.textColor = [UIColor whiteColor];
    [cell.contentView addSubview:titleLabel];

    UILabel *title1Label = [[UILabel alloc] initWithFrame:CGRectMake(110,100*screenMySize.size.width/320-20, screenMySize.size.width-15-108/2-100, 10)];
    title1Label.text = [NSString stringWithFormat:@"%@人关注",[allArray objectForKey:@"ShopFocusCount"]];
    title1Label.textAlignment = 0;
    title1Label.font = [UIFont fontWithName:@"Arial" size:labelSize-2];
    title1Label.textColor = [UIColor whiteColor];
    [cell.contentView addSubview:title1Label];

    gzBOOl = NO;
    gzButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [gzButton setFrame:CGRectMake(screenMySize.size.width-15-108/2, 100*screenMySize.size.width/320-40, 108/2, 49/2)];
    if ([[NSString stringWithFormat:@"%@",[allArray objectForKey:@"IsCustomerFocus"]] isEqualToString:@"1"])
    {
        [gzButton setImage:[UIImage imageNamed:@"ShopsGZ"] forState:UIControlStateNormal];
        gzBOOl = YES;
    }
    else
        [gzButton setImage:[UIImage imageNamed:@"ShopsWGZ"] forState:UIControlStateNormal];
    [gzButton setImage:[UIImage imageNamed:@"ShopsGZ"] forState:UIControlStateHighlighted];
    [gzButton addTarget:self action:@selector(gzAction) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:gzButton];
    
    for (int i = 0; i < 4; i ++)
    {
        if (i != 0)
        {
            UIView *b2View = [[UIView alloc] initWithFrame:CGRectMake(screenMySize.size.width*i/4,100*screenMySize.size.width/320+10, .5, aHight-20)];
            b2View.backgroundColor = [UIColor colorWithWhite:.8 alpha:1];
            [cell.contentView addSubview:b2View];
        }

        
        UIButton *bgButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [bgButton setFrame:CGRectMake(0, 0, screenMySize.size.width/4-2.5, aHight)];
//        [bgButton setBackgroundColor:[UIColor colorWithWhite:0 alpha:.1]];
        bgButton.center = CGPointMake((screenMySize.size.width+2.5)*i/4+screenMySize.size.width/(4*2),100*screenMySize.size.width/320+aHight/2);
        bgButton.tag = 100+i;
        [bgButton addTarget:self action:@selector(aInyBction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:bgButton];
        
        NSArray *titArray = [NSArray arrayWithObjects:@"全部商品",@"上新",@"促销",@"店铺动态", nil];

        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, screenMySize.size.width/4-2.5, 30)];
        titleLabel.text = @"0";
        titleLabel.textAlignment = 1;
//        HelveticaNeue-Bold
        titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize];
        titleLabel.textColor = [UIColor blackColor];
        [bgButton addSubview:titleLabel];
        if (i == 0) {
            titleLabel.text = [NSString stringWithFormat:@"%@",[allArray objectForKey:@"ProductTotalCount"]];
        }
        else if (i == 1) {
            //            titleLabel.text = [NSString stringWithFormat:@"%@",[allArray objectForKey:@"ProductTotalCount"]];
        }
        else if (i == 2) {
            titleLabel.text = [NSString stringWithFormat:@"%@",[allArray objectForKey:@"ProductActivityCount"]];
        }
        else if (i == 3) {
            //            titleLabel.text = [NSString stringWithFormat:@"%@",[allArray objectForKey:@"ProductTotalCount"]];
        }
        
        
        UILabel *title1Label = [[UILabel alloc] init];
        title1Label.frame = CGRectMake(0,30, screenMySize.size.width/4-2.5, 20);
        title1Label.text = [titArray objectAtIndex:i];
        title1Label.textAlignment = 1;
        title1Label.font = [UIFont fontWithName:@"Arial" size:labelSize-1];
        title1Label.textColor = [UIColor grayColor];
        [bgButton addSubview:title1Label];
        
        
    }
    
    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell bCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIScrollView *bScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, 70)];
    bScrollView.backgroundColor = [UIColor whiteColor];
    //    [bScrollView setDelegate:self];
    //    [bScrollView setPagingEnabled:YES];
    [bScrollView setShowsHorizontalScrollIndicator:NO];
    [cell.contentView addSubview:bScrollView];
    
    int type = 5;
    [bScrollView setContentSize:CGSizeMake(10+140*type,70)];
    
    for (int i = 0; i < type; i ++) {
        UIButton *gz1Button = [UIButton buttonWithType:UIButtonTypeCustom];
        [gz1Button setFrame:CGRectMake(10+140*i, 5, 130, 60)];
        [gz1Button setImage:[UIImage imageNamed:@"Shop_image1"] forState:UIControlStateNormal];
        gz1Button.tag = 110+i;
        [gz1Button addTarget:self action:@selector(hyjAction:) forControlEvents:UIControlEventTouchUpInside];
        [bScrollView addSubview:gz1Button];
    }
    
    
    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell cCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIScrollView *bScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, 150)];
    bScrollView.backgroundColor = [UIColor whiteColor];
//    [bScrollView setDelegate:self];
    [bScrollView setPagingEnabled:YES];
    [bScrollView setShowsHorizontalScrollIndicator:NO];
    [cell.contentView addSubview:bScrollView];
    
    int type = 5;
    [bScrollView setContentSize:CGSizeMake(self.view.bounds.size.width*type,150)];
    
    for (int i = 0; i < type; i ++) {
        UIButton *gz1Button = [UIButton buttonWithType:UIButtonTypeCustom];
        [gz1Button setFrame:CGRectMake((self.view.bounds.size.width-300)/2+self.view.bounds.size.width*i, 10, 300, 130)];
        [gz1Button setImage:[UIImage imageNamed:@"Shop_image"] forState:UIControlStateNormal];
        gz1Button.tag = 2000+i;
        [gz1Button addTarget:self action:@selector(ADAction:) forControlEvents:UIControlEventTouchUpInside];
        [bScrollView addSubview:gz1Button];
    }

    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell dCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell eCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell fCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
